﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using GUI;
using GUIServerConnectorAPI;
using EntityModel;
using LoginForm;

namespace GUI
{
    public partial class AdminForm : Form
    {
        //utilizate pt ListView SubItems editabile coloana a 2-a
        private ListViewHitTestInfo hitinfo;
        private TextBox editbox = new TextBox();
        //
        private LoginForm.LoginForm loginForm;
        private Guid sessionId;
        private AddUserForm addUserForm;
        private int tip; //utilizat pt radio button-uri,in functie de valoarea lui,se adauga diferite tipuri de useri
        private int tip2;//radio buton-uri,vizualizare useri

        public AdminForm(LoginForm.LoginForm loginForm)
        {
            InitializeComponent();
            this.loginForm = loginForm;
            editbox.Parent = listView;
            editbox.Hide();
            editbox.LostFocus += new EventHandler(editbox_LostFocus);
            listView.MouseDoubleClick += new MouseEventHandler(listView_MouseDoubleClick);
            listView.FullRowSelect = true;
            listView.LabelEdit = false;
        }

        public void Show(Guid sessionId)
        {
            this.sessionId = sessionId;
            Show();
        }

        public void Reset(Form form)
        {
            if (form == addUserForm)
                addUserForm = new AddUserForm(this);
            else
                throw new InvalidOperationException(Strings.IllegalResetRequestor);
            Show();
        }

        #region Edit Data in ListView

        private void AdminForm_Load(object sender, EventArgs e)
        {
            MedicradioButton.Checked = Enabled;
            MedicRadio2.Checked = Enabled;
        }

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
           
            hitinfo = listView.HitTest(e.X, e.Y);
            ListViewItem item = hitinfo.Item;           
          
            if(item.SubItems.IndexOf(hitinfo.SubItem).ToString() == "1")
            {
                editbox.Bounds = hitinfo.SubItem.Bounds;
                editbox.Text = hitinfo.SubItem.Text;          
                editbox.Show();
                editbox.Focus();
            }
             else
               listView.LabelEdit = false;      
                
        }
        void editbox_LostFocus(object sender, EventArgs e)
        {
            hitinfo.SubItem.Text = editbox.Text;
            editbox.Hide();
        }
#endregion

        #region Show Data in ListView
        public void ShowDatainListView(int type)
        {
            ListViewItem user = new ListViewItem("Utilizator:");
            user.SubItems.Add("");
            listView.Items.Add(user);
            ListViewItem parola = new ListViewItem("Parola:");
            parola.SubItems.Add("");
            listView.Items.Add(parola);
            if (type == 1)//se afiseaza in listview user de tip Medic
            {
                ListViewItem nume = new ListViewItem("Nume:");
                nume.SubItems.Add("");
                listView.Items.Add(nume);
                ListViewItem prenume = new ListViewItem("Prenume:");
                prenume.SubItems.Add("");
                listView.Items.Add(prenume);
                ListViewItem specializare = new ListViewItem("Specializare:");
                specializare.SubItems.Add("");
                listView.Items.Add(specializare);
                ListViewItem IDPacient = new ListViewItem("Id Pacient:");
                IDPacient.SubItems.Add("");
                listView.Items.Add(IDPacient);
            }
            else if (type == 2)//se afiseaza in litsview user de tip Pacient
            {
                ListViewItem idModul = new ListViewItem("Id modul:");
                idModul.SubItems.Add("");
                listView.Items.Add(idModul);
                ListViewItem nume = new ListViewItem("Nume:");
                nume.SubItems.Add("");
                listView.Items.Add(nume);
                ListViewItem prenume = new ListViewItem("Prenume:");
                prenume.SubItems.Add("");
                listView.Items.Add(prenume);
                ListViewItem cnp = new ListViewItem("CNP:");
                cnp.SubItems.Add("");
                listView.Items.Add(cnp);
                ListViewItem adresa = new ListViewItem("Adresa:");
                adresa.SubItems.Add("");
                listView.Items.Add(adresa);
                ListViewItem telefon = new ListViewItem("Telefon:");
                telefon.SubItems.Add("");
                listView.Items.Add(telefon);
                ListViewItem email = new ListViewItem("Email:");
                email.SubItems.Add("");
                listView.Items.Add(email);
                ListViewItem istMedical = new ListViewItem("Istoric Medical:");
                istMedical.SubItems.Add("");
                listView.Items.Add(istMedical);
                ListViewItem recoMedicale = new ListViewItem("Recomandari Medicale:");
                recoMedicale.SubItems.Add("");
                listView.Items.Add(recoMedicale);
                ListViewItem listAlergii = new ListViewItem("Lista Alergii:");
                listAlergii.SubItems.Add("");
                listView.Items.Add(listAlergii);
                ListViewItem masFiz = new ListViewItem("Masuri Fiziologice:");
                masFiz.SubItems.Add("");
                listView.Items.Add(masFiz);
            }
            else if (type == 3)//se afiseaza in listview user de tip Supraveghetor
            {
                ListViewItem nume = new ListViewItem("Nume:");
                nume.SubItems.Add("");
                listView.Items.Add(nume);
                ListViewItem prenume = new ListViewItem("Prenume:");
                prenume.SubItems.Add("");
                listView.Items.Add(prenume);
            }
            else if(type==4)//se afiseaza in listview user de tip Ingrijitor
            {
                ListViewItem nume = new ListViewItem("Nume:");
                nume.SubItems.Add("");
                listView.Items.Add(nume);
                ListViewItem prenume = new ListViewItem("Prenume:");
                prenume.SubItems.Add("");
                listView.Items.Add(prenume);
            }
        }
        #endregion

        #region AddUser Method
        public void AddUser(int type)
        {
            MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
            
            User user = new User();
            user.idUser = new Guid();
            user.Username = listView.Items[0].SubItems[0].Text;
            user.Password = md5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(listView.Items[1].SubItems[0].Text));
            if (type == 1)//se adauga user de tip Medic
            {
                Medic medic = new Medic();
                medic.idMedic = new Guid();
                medic.Nume = listView.Items[2].SubItems[0].Text;
                medic.Prenume = listView.Items[3].SubItems[0].Text;
                medic.Specializare = listView.Items[4].SubItems[0].Text;
             //   medic.idPacient = listView.Items[3].SubItems[0].ToString();

                user.Rol = AccountType.MEDIC.ToString();
                medic.User = user;
                GUIServerConnector.getInstance().getManagerInterface().addMedic(sessionId, medic);
               
            }
            else if (type == 2)//se adauga user de tip Pacient
            {
                Pacient pacient = new Pacient();
                pacient.idPacient = new Guid(md5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(listView.Items[2].SubItems[0].Text)));
                pacient.Nume = listView.Items[3].SubItems[0].Text;
                pacient.Prenume = listView.Items[4].SubItems[0].Text;
                pacient.CNP = listView.Items[5].SubItems[0].Text;
                pacient.Adresa=listView.Items[6].SubItems[0].Text;
                pacient.Telefon = listView.Items[7].SubItems[0].Text;
                pacient.Email = listView.Items[8].SubItems[0].Text;
                pacient.Istoric_medical=listView.Items[9].SubItems[0].Text;
                pacient.Recomandari_medicale = listView.Items[10].SubItems[0].Text;
                pacient.Lista_alergi = listView.Items[11].SubItems[0].Text;
                pacient.Masurari_fiziologice = listView.Items[12].SubItems[0].Text;

                user.Rol = AccountType.PACIENT.ToString();
                pacient.User = user;
                GUIServerConnector.getInstance().getManagerInterface().addPacient(sessionId, pacient);
            }
            else if (type == 3)//se adauga user de tip Supraveghetor
            {
                Supraveghetor supraveghetor = new Supraveghetor();
                supraveghetor.idSupraveghetor = new Guid();
                supraveghetor.Nume = listView.Items[2].SubItems[0].Text;
                supraveghetor.Prenume = listView.Items[3].SubItems[0].Text;

                user.Rol = AccountType.SUPRAVEGHETOR.ToString();
                supraveghetor.User = user;
                GUIServerConnector.getInstance().getManagerInterface().addSupraveghetor(sessionId, supraveghetor);
            }
            else if(type==4) //se adauga user de tip Ingrijitor
            {
                Ingrijitor ingrijitor = new Ingrijitor();
                ingrijitor.idIngrijitor = new Guid();
                ingrijitor.Nume = listView.Items[2].SubItems[0].Text;
                ingrijitor.Prenume = listView.Items[3].SubItems[0].Text;

                user.Rol = AccountType.INGRIJITOR.ToString();
                ingrijitor.User = user;
                GUIServerConnector.getInstance().getManagerInterface().addIngrijitor(sessionId, ingrijitor);
            }
        }
        #endregion

        private void MedicradioButton_CheckedChanged(object sender, EventArgs e)
        {
            tip = 1;
            listView.Items.Clear();
            ShowDatainListView(tip);
        }

        private void PacientradioButton_CheckedChanged(object sender, EventArgs e)
        {
            tip = 2;
            listView.Items.Clear();
            ShowDatainListView(tip);
        }

        private void SupraveghetorradioButton_CheckedChanged(object sender, EventArgs e)
        {
            tip = 3;
            listView.Items.Clear();
            ShowDatainListView(tip);
        }


        private void ADDbutton_Click(object sender, EventArgs e)
        {
            AddUser(tip);
            listView.Items.Clear();
        }

        private void IngrijitorRB_CheckedChanged_1(object sender, EventArgs e)
        {
            tip = 4;
            listView.Items.Clear();
            ShowDatainListView(tip);
        }
        private void ViewUserList(int type)
        {
            if (type == 1)
            {
                List<Medic> listaMedici = GUIServerConnector.getInstance().getManagerInterface().getMedics(sessionId);
                listView1.BeginUpdate();
                foreach (Medic medic in listaMedici)
                {                
                    ListViewItem item = new ListViewItem(medic.idUser.ToString());
                    item.SubItems.Add(medic.Nume.ToString());
                    item.SubItems.Add(medic.Prenume.ToString());
                    
                    listView1.Items.Add(item);
                }
                listView1.EndUpdate();
            }
            else if (type == 2)
            {
                List<Pacient> listaPacienti = GUIServerConnector.getInstance().getManagerInterface().getPacients(sessionId);
                listView1.BeginUpdate();
                foreach (Pacient pacient in listaPacienti)
                {
                    ListViewItem item = new ListViewItem(pacient.idUser.ToString());
                    item.SubItems.Add(pacient.Nume.ToString());
                    item.SubItems.Add(pacient.Prenume.ToString());

                    listView1.Items.Add(item);
                }
                listView1.EndUpdate();
            }
            else if (type == 3)
            {
                List<Supraveghetor> listaSupraveghetori = GUIServerConnector.getInstance().getManagerInterface().getSupraveghetors(sessionId);
                listView1.BeginUpdate();
                foreach (Supraveghetor supra in listaSupraveghetori)
                {
                    ListViewItem item = new ListViewItem(supra.idUser.ToString());
                    item.SubItems.Add(supra.Nume.ToString());
                    item.SubItems.Add(supra.Prenume.ToString());

                    listView1.Items.Add(item);
                }
                listView1.EndUpdate();
            }
            else if (type == 4)
            {
                List<Ingrijitor> listaIngrijitori = GUIServerConnector.getInstance().getManagerInterface().getIngrijitors(sessionId);
                listView1.BeginUpdate();
                foreach (Ingrijitor ingrijitor in listaIngrijitori)
                {
                    ListViewItem item = new ListViewItem(ingrijitor.idUser.ToString());
                    item.SubItems.Add(ingrijitor.Nume.ToString());
                    item.SubItems.Add(ingrijitor.Prenume.ToString());

                    listView1.Items.Add(item);
                }
                listView1.EndUpdate();
            }
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            tip2 = 1;
            listView1.Items.Clear();
            ViewUserList(tip2);
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            tip2 = 2;
            listView1.Items.Clear();
            ViewUserList(tip2);
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            tip2 = 3;
            listView1.Items.Clear();
            ViewUserList(tip2);
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            tip2 = 4;
            listView1.Items.Clear();
            ViewUserList(tip2);
        }

        private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
        }

        private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView.Columns[e.ColumnIndex].Width;
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void AdminForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            GUIServerConnector.getInstance().getAuthenticator().logout(sessionId);
            this.Hide();
            loginForm.Reset(this);
        }
    }
}
